<template>
    <n-drawer :show="active" :width="width" :mask-closable="false" @update:show="(flag:boolean) => (active = flag)">
        <n-drawer-content :native-scrollbar="false" title="开具发票" closable>
            <n-space vertical :size="12" v-if="inv.id">
                <n-descriptions label-placement="left" title="基本信息" bordered :column="1">
                    <n-descriptions-item :label-style="labelStyle" label="开票ID">
                        <n-text code>{{ inv.id }}</n-text>
                    </n-descriptions-item>
                    <n-descriptions-item :label-style="labelStyle" label="开票金额">{{ Filters.money(inv.amount) }}</n-descriptions-item>
                    <n-descriptions-item :label-style="labelStyle" label="申请时间">{{ inv.apply }}</n-descriptions-item>
                    <n-descriptions-item :label-style="labelStyle" label="开票状态">
                        <n-text v-if="inv.state == 1">门店处理中</n-text>
                        <n-text v-if="inv.state == 2">已委托代开</n-text>
                        <n-text v-if="inv.state == 3">已完成开票</n-text>
                    </n-descriptions-item>
                    <n-descriptions-item :label-style="labelStyle" label="发票类型">{{ inv.invoice.type == 1 ? '增值税普通发票' : '增值税专用发票' }}</n-descriptions-item>
                    <n-descriptions-item :label-style="labelStyle" label="抬头类型">{{ inv.invoice.person ? '个人' : '单位' }}</n-descriptions-item>
                    <n-descriptions-item :label-style="labelStyle" label="抬头名称">{{ inv.invoice.name }}</n-descriptions-item>
                    <n-descriptions-item :label-style="labelStyle" label="公司税号" v-if="!inv.invoice.person">{{ inv.invoice.taxno }}</n-descriptions-item>
                    <n-descriptions-item :label-style="labelStyle" label="开户行" v-if="!inv.invoice.person">{{ inv.invoice.bank }}</n-descriptions-item>
                    <n-descriptions-item :label-style="labelStyle" label="银行账户" v-if="!inv.invoice.person">{{ inv.invoice.card }}</n-descriptions-item>
                    <n-descriptions-item :label-style="labelStyle" label="注册地址" v-if="!inv.invoice.person">{{ inv.invoice.addr }}</n-descriptions-item>
                    <n-descriptions-item :label-style="labelStyle" label="联系电话" v-if="!inv.invoice.person">{{ inv.invoice.phone }}</n-descriptions-item>
                </n-descriptions>

                <n-descriptions label-placement="left" title="邮寄信息" bordered :column="1">
                    <n-descriptions-item :label-style="labelStyle" label="邮寄类型">{{ inv.invoice.ele ? '电子发票' : '纸质发票' }}</n-descriptions-item>
                    <n-descriptions-item :label-style="labelStyle" label="邮箱地址" v-if="inv.invoice.ele">{{ inv.invoice.email }}</n-descriptions-item>
                    <n-descriptions-item :label-style="labelStyle" label="邮寄地址" v-if="!inv.invoice.ele">{{ inv.invoice.post }}</n-descriptions-item>
                </n-descriptions>

                <n-divider />
                <n-form :model="model" ref="formRef" v-if="!inv.invoice.ele">
                    <n-form-item label="快递名称">
                        <n-input placeholder="请填写快递名称" v-model:value="model.name" maxlength="32" show-count clearable />
                    </n-form-item>
                    <n-form-item label="快递单号">
                        <n-input placeholder="请填写快递单号" v-model:value="model.sno" maxlength="64" show-count clearable />
                    </n-form-item>
                </n-form>
            </n-space>
            <template #footer>
                <n-space justify="end">
                    <n-button type="tertiary" @click="() => (active = false)">
                        <template #icon>
                            <n-icon :component="CloseRound" />
                        </template>
                        关闭
                    </n-button>
                    <n-button type="primary" :loading="loading" @click="execSave">
                        <template #icon>
                            <n-icon :component="CheckRound" />
                        </template>
                        提交开具
                    </n-button>
                </n-space>
            </template>
        </n-drawer-content>
    </n-drawer>
</template>

<script lang="ts" setup>
import { computed, ref, onBeforeMount } from 'vue'
import { CloseRound, CheckRound } from '@vicons/material'

import { Http, Filters } from '@/libs'
import { useStore } from '@/store'

const store = useStore()
const width = computed(() => Math.max(store.state.browser.innerWidth * 0.4, 640))

const { $notification: notification } = window

const loading = ref(false)

const labelStyle = {
    width: '150px',
}
const prop = defineProps({
    show: {
        type: Boolean,
        required: true,
    },
    item: {
        type: Object,
        default: {},
    },
})

const emit = defineEmits(['update:show', 'refresh'])

const active = computed({
    get: () => prop.show,
    set: val => emit('update:show', val),
})

const formRef = ref<any>(null)

const model = ref<any>({})

const inv = ref<any>({})

const execLoad = () => {
    Http.post('/ops/fin/invoice/detail', { id: prop.item.id }).then(resp => {
        if (resp.stat.err > 0) {
            return
        }
        inv.value = resp.data
    })
}

const execSave = (e: Event) => {
    if (!inv.value.invoice.ele) {
        if (!model.value.name) {
            notification.error({
                duration: 1500,
                title: '请填写完整内容',
                description: '请填写快递名称',
            })
            return
        }
        if (!model.value.sno) {
            notification.error({
                duration: 1500,
                title: '请填写完整内容',
                description: '请填写快递单号',
            })
            return
        }
    }
    e.preventDefault()
    formRef.value.validate((errors: any[]) => {
        if (!errors) {
            loading.value = true
            Http.post('/ops/fin/invoice/issue', {
                id: inv.value.id,
                exp: model.value,
            }).then(resp => {
                if (resp.stat.err > 0) {
                    loading.value = false
                    return
                }
                emit('refresh', {})
                notification.success({
                    duration: 1500,
                    title: '操作成功',
                    content: '您已成功提交开票',
                })
                setTimeout(() => {
                    loading.value = false
                    emit('update:show', false)
                }, 200)
            })
        } else {
            let msgs = errors.map(err => err[0].message)
            notification.error({
                duration: 1500,
                title: '请填写完整内容',
                description: '当前表单存在以下问题，请处理后提交',
                content: msgs.join('\n'),
            })
        }
    })
}

onBeforeMount(() => {
    execLoad()
})
</script>
